// 导入http模板
const fs = require('http')

// 创建服务对象
const server = fs.createServer((req, res) => {
  // 接收客户端请求
  console.log(req.url) 

  // 获取请求方法
  console.log(req.method);  // GET

  // 获取请求头
  console.log(req.headers);
  `/*
    {
    host: 'localhost:9000',
    connection: 'keep-alive',
    'cache-control': 'max-age=0',
    'sec-ch-ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
    accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'sec-fetch-site': 'none',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-user': '?1',
    'sec-fetch-dest': 'document',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9'
    }
  */`

  // 响应客户端，设置响应体
  res.setHeader('Content-Type', 'text/html;charset=utf-8')
  res.end('你好 HTTP Server')
})

// 监听端口，启动服务
server.listen(9000, () => {
  console.log('服务器启动成功')
})